﻿using FTBS.Models.BLO;
using FTBS.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace FTBS.Controllers
{
    public class CategoryController : Controller
    {
        CategoryBLO _bloCategory = new CategoryBLO();

        public ActionResult Index()
        {
            if (this.Session["RoleName"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return HttpNotFound();
            }
            return View(_bloCategory.LoadCategories());
        }

        public ActionResult Create()
        {
            if (this.Session["RoleName"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return HttpNotFound();
            }
            return View();
        }

        [HttpPost]
        public ActionResult Create(Category cate)
        {
            if (ModelState.IsValid)
            {
                if (!_bloCategory.CheckIsExisted(cate))
                {
                    ViewBag.categoryName = null;
                    _bloCategory.CreateCategory(cate);
                    return RedirectToAction("Index");
                }
                ViewBag.cateName = cate.CategoryName;
                return View(cate);
            }
            return View(cate);
        }

        public ActionResult Edit(int id = 0)
        {
            if (this.Session["RoleName"] == null || this.Session["RoleName"].ToString().Trim() != "Staff")
            {
                return HttpNotFound();
            }
            ViewBag.cateName = null;
            Category cate = _bloCategory.LoadCategoryById(id);
            if (cate == null)
            {
                return HttpNotFound();
            }
            return View(cate);
        }

        [HttpPost]
        public ActionResult Edit(Category cate)
        {
            var id = cate.CategoryId;
            if (ModelState.IsValid)
            {
                if (!_bloCategory.CheckIsExisted(cate))
                {
                    ViewBag.cateName = null;
                    _bloCategory.EditCategory(cate);
                    return RedirectToAction("Index");
                }
                if (cate.CategoryName.Equals(_bloCategory.LoadCategoryById(id).CategoryName))
                {
                    return RedirectToAction("Index");
                }
                ViewBag.cateName = cate.CategoryName;
                return View(cate);
            }
            return View(cate);
        }

        /// <summary>
        /// delete a category film
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <author>hungph</author>
        [HttpDelete, ActionName("Delete")]
        public ActionResult Delete(int id = 0)
        {
            Category category = _bloCategory.LoadCategoryById(id);
            if (category == null)
            {
                return HttpNotFound();
            }
            _bloCategory.DeleteCategory(category);
            return RedirectToAction("Index");
        }
    }
}
